package icu.makka.yygh.user.api;

import icu.makka.yygh.model.user.UserInfo;
import icu.makka.yygh.result.Result;
import icu.makka.yygh.result.helper.AuthContextHolder;
import icu.makka.yygh.user.service.UserInfoService;
import icu.makka.yygh.user.service.impl.UserInfoServiceImpl;
import icu.makka.yygh.vo.user.LoginVo;
import icu.makka.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/api/user")
@RestController
public class UserInfoApiController {

    @Autowired
    private UserInfoServiceImpl userInfoService;

    /**
     * 用户登录
     * @param loginVo
     * @return
     */
    @ApiOperation(value = "会员登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        log.info("用户登录，loginVo={}", loginVo);
        Map<String, Object> info = userInfoService.login(loginVo);
        return Result.ok(info);
    }

    /**
     * 用户认证接口
     * @param userAuthVo
     * @param request
     * @return
     */
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        log.info("用户认证，usrAuthVo={}", userAuthVo);
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    /**
     * 根据用户id获取用户信息
     * @param request
     * @return
     */
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        log.info("根据用户id获取用户信息接口");

        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);

        Map<String, Object> map = new HashMap<>();
        if ("10".equals(userInfo.getCertificatesType())) {
            map.put("certificatesTypeString", "身份证");
        } else {
            map.put("certificatesTypeString", "户口本");
        }
        userInfo.setParam(map);

        return Result.ok(userInfo);
    }
}
